package com.hp.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hp.context.Config;
import com.hp.dao.MenusDao;
import com.hp.dao.PowerDao;
import com.hp.dao.RoleDao;
import com.hp.dao.UserDao;
import com.hp.entity.Menus;
import com.hp.entity.Power;
import com.hp.entity.Role;
import com.hp.entity.User;
import com.hp.result.DataGridResultInfo;
import com.hp.result.ResultUtil;
import com.hp.result.SubmitResultInfo;
import com.hp.service.RoleService;
import com.hp.service.UserService;
import com.hp.utils.Md5Encryption;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Transactional
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private MenusDao menusDao;
    @Autowired
    private PowerDao powerDao;




    @Transactional(readOnly = true)
    @Override
    public DataGridResultInfo page(int page, int rows) {
        Page<User> startPage = PageHelper.startPage(page, rows);//使用分页插件方法
        userDao.findAll();//插件方法必须在查询数据之前
        //封装数据
        DataGridResultInfo dataGridResultInfo = new DataGridResultInfo();
        dataGridResultInfo.setRows(startPage.getResult());//当前查询的数据
        dataGridResultInfo.setTotal((int) startPage.getTotal());//总页数
        return dataGridResultInfo;
    }

    @Override
    public SubmitResultInfo adduser(User user) throws Exception {
        User u= userDao.findByUserName(user.getUsername());
        if(u!=null){
            return ResultUtil.createSubmitResult(ResultUtil.createFail(Config.MESSAGE, 669, new Object[]{"用户"}));

        }
        user.setPassword(Md5Encryption.Md5Encryption(user.getPassword()));
        userDao.addUser(user);
        return ResultUtil.createSubmitResult(ResultUtil.createSuccess(Config.MESSAGE, 661, new Object[]{"用户"}));

    }
    @Transactional(readOnly = true)
    @Override
    public User findById(int id) {
        return userDao.findById(id);
    }

    @Override
    public SubmitResultInfo authrole(int userid, int roleid) {

          userDao.authRole(userid,roleid);

        return ResultUtil.createSubmitResult(ResultUtil.createSuccess(Config.MESSAGE, 666, new Object[]{"用户角色"}));

    }

    /**
     * 登入
     * @param user
     * @param session
     * @param request
     * @param response
     * @return
     */
    @Transactional(readOnly = true)
    @Override
    public String dologin(User user, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws Exception{
        user.setPassword(Md5Encryption.Md5Encryption(user.getPassword()));
        User u = userDao.findByUsernameAndPassword(user);
        if(u==null){
          response.sendRedirect(request.getContextPath()+"/login.jsp");
          return null;
        }else{
            Role role = roleDao.findById(u.getRoleid());//获取用户对应的角色
            List<Menus> menusList = menusDao.findAllByRoleId(role.getId());//获取角色对应的菜单
            List<Power> powerList = powerDao.findAllByRoleId(role.getId());//获取角色对应的功能
            session.setAttribute("user",u);
            session.setAttribute("r",role);
            session.setAttribute("menusList",menusList);
            session.setAttribute("powerList",powerList);
            response.sendRedirect(request.getContextPath()+"/main");
        }
        return null;
    }

    @Transactional(readOnly = false)
    @Override
    public int updatePwd(User user) throws Exception {
        user.setPassword(Md5Encryption.Md5Encryption(user.getPassword()));
        return userDao.updatePwd(user);
    }
}
